<template>
	<view class="content wrap">
		<scan></scan>
		<scanner></scanner>
		<sm></sm>
		<honeywell></honeywell>
		<u-form :model="form" ref="uForm">
			<u-form-item label="发货地" label-width="150" prop="srcPositionCode">
				<u-row>
					<u-col span="11" text-align="center">
						<u-input v-model="form.srcPositionCode" placeholder="请输入发货地" />
					</u-col>
					<u-col span="1" text-align="center" style="display: flex; align-items: center;" @click="scan">
						<u-icon name="scan" color="#2b85e4" size="43" style="font-size: 80px;">手机扫描</u-icon>
					</u-col>
				</u-row>
			</u-form-item>

			<u-form-item label="收货地" label-width="150" prop="tarPositionCode">
				<u-row>
					<u-col span="11" text-align="center">
						<u-input v-model="form.tarPositionCode" placeholder="请输入收货地" />
					</u-col>
					<u-col span="1" text-align="center" style="display: flex; align-items: center;" @click="scan1">
						<u-icon name="scan" color="#2b85e4" size="43" style="font-size: 80px;">手机扫描</u-icon>
					</u-col>
				</u-row>
			</u-form-item>
			<!-- <u-form-item label="工作区域" label-width="150" prop="tarPositionCode">
				<u-row>
					<u-col span="12" text-align="center">
						<u-input v-model="taskTypName" placeholder="选择区域" type="select" @click="getO()"/>
					</u-col>
				</u-row>
			</u-form-item> -->

		</u-form>
		<view>
			<u-form-item>
			</u-form-item>
		</view>
		<u-button @click="clockin" type="primary">提交</u-button>
		<view>
			<u-toast ref="uToast" />
		</view>
		<view>
			<u-select v-model="uSelectShow" :list="areaList" @confirm="confirm"></u-select>
		</view>
	</view>
</template>

<script>
	import scan from "@/components/uni-scan/scan.vue"
	import scanner from "@/components/uni-scan/scanner.vue"
	import sm from "@/components/uni-scan/sm.vue"
	import honeywell from "@/components/uni-scan/honeywell.vue"

	export default {
		components: {
			scanner,
			scan,
			sm,
			honeywell
		},
		data() {
			return {
				uSelectShow:false,
				taskTypName:'1区',
				form: {
					clientCode: "小程序", //小程序
					srcPositionCode: "", //开始库位
					tarPositionCode: "", //目标位置
					taskTyp:'F14', //目标位置
				},
				scanneal: [],
				areaList: [
					{
						value:'F14',
						label:'1区'
					},
					{
						value:'F23',
						label:'2区'
					},
					{
						value:'F24',
						label:'3区'
					},
					{
						value:'F25',
						label:'4区'
					},
				],
			};
		},
		onShow() {
			let srcPositionCodeObtained = false;
			let tarPositionCodeObtained = false;

			uni.$off('scan');
			uni.$on('scan', (res) => {
				console.log('扫码结果：', res.codeStr);

				if (!srcPositionCodeObtained) {
					this.form.srcPositionCode = res.codeStr;
					srcPositionCodeObtained = true;
					if (tarPositionCodeObtained) {
						tarPositionCodeObtained = false; // 如果this.form.tarPositionCode已经被赋值过，则将它的标志重置
					}
				} else if (!tarPositionCodeObtained) {
					if (res.codeStr.includes('{')) {
						let match = res.codeStr.match(/{(.*?)}/); // 使用正则表达式匹配{}中的内容
						if (match) {
							let dataInBraces = match[1]; // 获取{}中的数据
							let parsedData = JSON.parse('{' + dataInBraces + '}'); // 解析{}中的JSON数据
							if (parsedData && parsedData['channel_name']) {
								let channel_name = parsedData['channel_name'];
								this.form.tarPositionCode = +channel_name.substring(0, 3);
								
								// 进行后续处理
							} else {
								console.log('无法获取商品条码字段');
							}
						} else {
							console.log('未找到{}中的数据');
						}
					} else {
						console.log('直接使用扫描到的数据：', res.codeStr);
						this.form.tarPositionCode = res.codeStr;

					}

					tarPositionCodeObtained = true;
					if (srcPositionCodeObtained) {
						srcPositionCodeObtained = false; // 如果this.form.srcPositionCode已经被赋值过，则将它的标志重置
					}
				}
			});

			uni.$off('scanner') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scanner', (res) => {
				console.log('扫码结果：', res.code);
				if (!srcPositionCodeObtained) {
					this.form.srcPositionCode = res.code;
					srcPositionCodeObtained = true;
					if (tarPositionCodeObtained) {
						tarPositionCodeObtained = false; // 如果this.form.tarPositionCode已经被赋值过，则将它的标志重置
					}
				} else if (!tarPositionCodeObtained) {
					if (res.code.includes('{')) {
						let match = res.code.match(/{(.*?)}/); // 使用正则表达式匹配{}中的内容
						if (match) {
							let dataInBraces = match[1]; // 获取{}中的数据
							let parsedData = JSON.parse('{' + dataInBraces + '}'); // 解析{}中的JSON数据
							if (parsedData && parsedData['channel_name']) {
								let channel_name = parsedData['channel_name'];
								this.form.tarPositionCode = +channel_name.substring(0, 3);
								// 进行后续处理
							} else {
								console.log('无法获取商品条码字段');
							}
						} else {
							console.log('未找到{}中的数据');
						}
					} else {
						console.log('直接使用扫描到的数据：', res.code);
						this.form.tarPositionCode = res.code;
					}

					tarPositionCodeObtained = true;
					if (srcPositionCodeObtained) {
						srcPositionCodeObtained = false; // 如果this.form.srcPositionCode已经被赋值过，则将它的标志重置
					}
				}
			})
			uni.$off('sm') // 每次进来先 移除全局自定义事件监听器
			uni.$on('sm', (res) => {
				console.log('扫码结果：', res.code);
				if (!srcPositionCodeObtained) {
					this.form.srcPositionCode = res.code;
					srcPositionCodeObtained = true;
					if (tarPositionCodeObtained) {
						tarPositionCodeObtained = false; // 如果this.form.tarPositionCode已经被赋值过，则将它的标志重置
					}
				} else if (!tarPositionCodeObtained) {
					if (res.code.includes('{')) {
						let match = res.code.match(/{(.*?)}/); // 使用正则表达式匹配{}中的内容
						if (match) {
							let dataInBraces = match[1]; // 获取{}中的数据
							let parsedData = JSON.parse('{' + dataInBraces + '}'); // 解析{}中的JSON数据
							if (parsedData && parsedData['channel_name']) {
								let channel_name = parsedData['channel_name'];
								this.form.tarPositionCode = +channel_name.substring(0, 3);
								// 进行后续处理
							} else {
								console.log('无法获取商品条码字段');
							}
						} else {
							console.log('未找到{}中的数据');
						}
					} else {
						console.log('直接使用扫描到的数据：', res.code);
						this.form.tarPositionCode = res.code;
					}
					tarPositionCodeObtained = true;
					if (srcPositionCodeObtained) {
						srcPositionCodeObtained = false; // 如果this.form.srcPositionCode已经被赋值过，则将它的标志重置
					}
				}
			})
			uni.$off('honeywell') // 每次进来先 移除全局自定义事件监听器
			uni.$on('honeywell', (res) => {
				console.log('扫码结果：', res.code);
				if (!srcPositionCodeObtained) {
					this.form.srcPositionCode = res.code;
					srcPositionCodeObtained = true;
					if (tarPositionCodeObtained) {
						tarPositionCodeObtained = false; // 如果this.form.tarPositionCode已经被赋值过，则将它的标志重置
					}
				} else if (!tarPositionCodeObtained) {
					if (res.code.includes('{')) {
						let match = res.code.match(/{(.*?)}/); // 使用正则表达式匹配{}中的内容
						if (match) {
							let dataInBraces = match[1]; // 获取{}中的数据
							let parsedData = JSON.parse('{' + dataInBraces + '}'); // 解析{}中的JSON数据
							if (parsedData && parsedData['channel_name']) {
								let channel_name = parsedData['channel_name'];
								this.form.tarPositionCode = +channel_name.substring(0, 3);
								// 进行后续处理
							} else {
								console.log('无法获取商品条码字段');
							}
						} else {
							console.log('未找到{}中的数据');
						}
					} else {
						console.log('直接使用扫描到的数据：', res.code);
						this.form.tarPositionCode = res.code;
					}
					tarPositionCodeObtained = true;
					if (srcPositionCodeObtained) {
						srcPositionCodeObtained = false; // 如果this.form.srcPositionCode已经被赋值过，则将它的标志重置
					}
				}
			})
		},

		methods: {
			scan() {
				uni.scanCode({
					success: (res) => {
						this.form.srcPositionCode = res.result;
					}
				});
			},
			scan1() {
				uni.scanCode({
					success: (res) => {
						if (res.result.includes('{')) {
							let match = res.result.match(/{(.*?)}/); // 使用正则表达式匹配{}中的内容
							if (match) {
								let dataInBraces = match[1]; // 获取{}中的数据
								let parsedData = JSON.parse('{' + dataInBraces + '}'); // 解析{}中的JSON数据
								if (parsedData && parsedData['channel_name']) {
									let channel_name = parsedData['channel_name'];
									this.form.tarPositionCode = +channel_name.substring(0, 3);
									// 进行后续处理
								} else {
									console.log('无法获取商品条码字段');
								}
							} else {
								console.log('未找到{}中的数据');
							}
						} else {
							console.log('直接使用扫描到的数据：', res.result);
							this.form.tarPositionCode = res.result;


						}
					}
				});
			},
			getO(){
				this.uSelectShow = true
			},
			confirm(e) {
				this.form.taskTyp = e[0].value;
				this.taskTypName = e[0].label;
			},
			clockin() {

				if (typeof this.form.srcPositionCode == 'undefined' || this.form.srcPositionCode == null || this.form
					.srcPositionCode ==
					'') {
					uni.showToast({
						title: '请输入发货地',
						duration: 2000,
						icon: 'none',
					});
					return false;
				}
				if (typeof this.form.tarPositionCode == 'undefined' || this.form.tarPositionCode == null || this.form
					.tarPositionCode == '') {
					uni.showToast({
						title: '请输入收货地',
						duration: 2000,
						icon: 'none',
					});
					return false;
				}
				uni.showLoading({
					title: '正在提交'
				})

				this.$http.post('/rest/hikAgvTaskController/addItemcc', {}, {
					params: this.form,
					header: {

						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading()
					if (res.data.code == 200) {
						this.$refs.uToast.show({
							title: '操作成功',
							duration: 3500,
							type: 'success'
						})
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							duration: 3500,
							type: 'error'
						})
					}
				})
			},
			// 选中某个单选框时，由radio时触发
			radioChange(e) {
				console.log(e);
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ffffff;
	}

	.wrap {
		padding: 30rpx;
	}
</style>
